package cn.YstHome.controller;

import cn.Product.service.ProductService;
import cn.User.service.UserService;
import cn.YstHome.service.YstHomeService;
import cn.controller.AopController;
import cn.util.aop.Authority;
import com.alibaba.fastjson.JSON;
import cn.util.json.JacksonUtil;
import common.constant.ServiceResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class YstHomeController {

    private Logger logger=LoggerFactory.getLogger(AopController.class);


    /**
     * 首页接口
     */
    @Autowired
    YstHomeService ystHomeService;

    /**
     * 产品接口类
     */
    @Autowired
    ProductService productService;

    /**
     * 用户接口
     */
    @Autowired
    UserService userService;


    /**
     * 首页
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("index.html")
    @Authority()
    public Object  Index(@RequestBody String json,Model model)  {
        logger.info("访问地址===index.jsp;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取当前主页信息
            map.put("DHID",1);
            result.put("YstInfo",ystHomeService.getYstInfo(map));
            //获取导航信息
            result.put("YstInfoNav",ystHomeService.getYstInfoNav(map));
            //商品数据
            result.put("ProductList",productService.getProductList(map));
            //商品类型
            result.put("ProductType",productService.getProductType(map));
            //菜谱:其实就是商品搭配列表
            result.put("ProductArray",productService.getProductArray(map));
            //获取登录用户信息
            result.put("YstUserInfo",userService.getuserinfo(map));
            //返回模板
            map.put("path","index");
        } catch (Exception e) {
            e.printStackTrace();
        }


        return ServiceResult.createResult(model,map,result);

    }

    /**
     * 获取导航信息
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("YstInfoNav.html")
    @Authority()
    public Object  getYstInfoNav(@RequestBody String json,Model model)  {
        logger.info("访问地址===YstInfoNav.html;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //商品数据
            result.put("YstInfoNav",ystHomeService.getYstInfoNav(map));
            //商品类型
            //返回模板
            map.put("path","YstInfoNav");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);

    }

    /**
     * 进入登陆界面
     * @param json
     * @param model
     * @return
     */
    @RequestMapping(value = "/login.html")
    @ResponseBody
    @Authority()
    public Object  Login(@RequestBody String json, Model model, @RequestParam(value = "url",required = false,defaultValue = "") String URL, HttpServletRequest request) {
        logger.info("访问地址===login.html;访问参数===" + json);
        //当前用户线程
        Subject subject=SecurityUtils.getSubject();
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //已登录
            if(subject.getPrincipal()!=null){
                //返回模板
                map.put("path", "redirect:/"+URL);
            }else{
                //返回模板
                map.put("path", "login");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }




}
